<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/25
 * Time: 16:37
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use ttcommon\base\Arrays;
use wechat\services\WeSystemVar;
use yii\db\Migration;

/**
 * 模板变量
 * @property string $name
 * @property integer $type
 * @property string $value
 */
class WeVar extends TtActiveRecord
{
    public static function tableName()
    {
        return 'we_var';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'name' => $migration->string(30)->notNull()->unique(),
            'type' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->unsigned(),
            'value' => $migration->string(255)->notNull()->defaultValue('')
        ]);
    }

    public function attributeLabels()
    {
        return [
            'name' => '变量名',
            'type' => '类型',
            'value' => '值'
        ];
    }

    public function rules()
    {
        return [
            ['type', 'integer'],
            ['type', 'default', 'value' => 0],
            ['name', 'match', 'pattern' => '#^[a-z]+$#', 'message' => $this->getAttributeLabel('name') . '只能使用小写字母'],
        ];
    }

// 表达式目前由系统提供
    const TYPE_TEXT = 0;
    const TYPE_EXPRESSION = 1;

    public static function typeText($type = null)
    {
        static $text = [
            self::TYPE_TEXT => '文字',
            self::TYPE_EXPRESSION => '表达式' // 暂时不支持
        ];
        return Arrays::constText($text, $type);
    }

    public function scenarios()
    {
        return [self::SCENARIO_DEFAULT => ['name', 'type', 'value']];
    }

}